package com.dailyblue.java.mybatis.mybatis06.mapper;

import com.dailyblue.java.mybatis.mybatis06.bean.Dept;
import com.dailyblue.java.mybatis.mybatis06.bean.Emp;
import org.apache.ibatis.annotations.*;

import java.util.List;
import java.util.Set;

@CacheNamespace(blocking = true)
public interface EmpMapper {
    @Select("select * from emp where empstate=1")
    List<Emp> find();

    @Insert("insert into emp values(null,#{ename},#{job},#{mgr},Now(),#{sal},#{comm},#{deptNo},1)")
    void save(Emp emp);

    @Update("update emp\n" +
            "        set ename=#{ename},\n" +
            "            job=#{job},\n" +
            "            mgr=#{mgr},\n" +
            "            sal=#{sal},\n" +
            "            comm=#{comm},\n" +
            "            deptno=#{deptNo}\n" +
            "        where empno = #{empNo}")
    void update(Emp emp);

    @Update("update emp set empstate=0 where empno=${id}")
    void delete(int id);

    // 关系  多对一
    @Select("select * from emp where empstate=1 and empno=#{id}")
    @Results({
            @Result(property = "empNo", column = "empno", id = true),
            @Result(property = "ename", column = "ename"),
            @Result(property = "dept", column = "deptno", javaType = Dept.class,
                    one = @One(select = "com.dailyblue.java.mybatis.mybatis06.mapper.DeptMapper.findDeptByNo"))
    })
    Emp find1(int id);

    @Select("select * from emp where empstate=1 and deptno=#{deptNo}")
    Set<Emp> findEmpByDeptNo(int deptNo);

    // 动态SQL 不推荐注解
    @Select("<script>select * from emp <where><if test='job!=null'>and job=#{job}</if></where></script>")
    List<Emp> find2(String job);
}
